package com.example.demo.controller.demo;

import com.example.demo.entity.CollectionName;
import com.example.demo.entity.Result;
import com.example.demo.service.comm.AggregationUtil;
import com.example.demo.service.comm.JsonUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * @description:
 * @author: cj
 * @create: 14-12-22 14:40:22
 */
@RestController
@RequestMapping({"/demo", "/token/demo"})
public class DemoController {

    @Resource
    MongoTemplate mongoTemplate;
    @Value("${spring.data.mongodb.database}")
    String dataBaseName;
    @Resource
    MongoClient mongoClient;
    MongoDatabase database;

    @PostConstruct
    public void setDatabase() {
        database = mongoClient.getDatabase( dataBaseName );
    }

    @PostMapping("/demo1")
    @PreAuthorize("hasAuthority('test')")
    public Result demo1() {

        List<Map> all = mongoTemplate.findAll( Map.class, CollectionName.USER );
        return Result.ok( all );
    }

    @GetMapping("/demo2")
    public Result demo2() {
        Map<String, Object> map = new HashMap<>( 30 );
        map.put( "name", "sss" );
        map.put( "name0", "sss" );
        map.put( "name1", "sss" );
        List<Map> all = mongoTemplate.findAll( Map.class, CollectionName.T_OBJECT );
        Set<String> collectionNames = mongoTemplate.getCollectionNames();
//        Map<String, Object> insert = mongoTemplate.insert( map, CollectionName.T_OBJECT );
        return Result.ok( all );
    }

    @GetMapping("/demo3")
    public Result demo3() {
        System.out.println( "" );
        return Result.ok( "demo3" );
    }

    @PostMapping("/demo4")
    public Result demo4(String name, String password) {
        System.out.println( name + password );
        return Result.ok( "demo4" );
    }

    @PostMapping("/demo4_1")
    public Result demo4(@RequestBody Map<String, Object> map) {
        System.out.println( map );
        return Result.ok( "demo4" );
    }

    @PostMapping("/demo5")
    public Result demo5() {
        System.out.println( "" );
        return Result.ok( "demo5" );
    }


    @GetMapping("/json1")
    public Result json1() {
        Map<String, Object> map = new HashMap<>( 2 );
        map.put( "price", 3 );
        map.put( "applyDiscount", true );
        BasicDBObject basicDBObject = JsonUtil.basicDBObject( "HomePageService.json", "findById", map );
        BasicDBObject query = new BasicDBObject( "price", 10 ).append( "tax", 0.5 );
        FindIterable<Document> documents = database.getCollection( CollectionName.TEST )
                .find( basicDBObject );
        MongoCursor<Document> cursor = documents.iterator();
        List<Map<String, Object>> result = new ArrayList();
        cursor.forEachRemaining( (action) -> {
            action.put( "_id", action.get( "_id" ).toString() );
            result.add( action );
        } );
        return Result.ok( result );
    }


    public static void main(String[] args) throws IOException {
        final StringBuilder builder = new StringBuilder();
        builder.append( new char[]{'q', 'b', '"', '"', 's', '"'}, 0, 4 );
        builder.append( Integer.valueOf( 4 ) );
        System.out.println( builder.toString() );
//        ///D:/cj/code/idea/demo/target/classes/json/HomePageService.json
//        String path = Class.class.getClass().getResource( "/" ).getPath();
//        System.out.println( path );
    }

}
